package com.veo.dao.Impl;

import com.veo.dao.ReaderDao;
import com.veo.entity.Books;
import com.veo.entity.User;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("readerDao")
public class ReaderDaoImpl extends HibernateDaoSupport implements ReaderDao {

    @Resource(name = "sessionFactory")
    public void setSessionFactory1(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<User> getAllReader() {
        List<User> reader=getHibernateTemplate().loadAll(User.class);
        return reader;
    }

    @Override
    public void deleteReader(User reader) {
        getHibernateTemplate().delete(reader);
    }

    @Override
    public void addReader(User reader) {
        getHibernateTemplate().save(reader);
        getHibernateTemplate().flush();

    }

    @Override
    public void updateReader(User reader) {
        getHibernateTemplate().update(reader);
        getHibernateTemplate().flush();
    }

    @Override
    public boolean findIdCacr(User reader) {
        List<User> list = (List<User>) getHibernateTemplate().find("from User where idCard='" + reader.getIdCard()+"'");
        if (list.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean findUsername(User reader) {
        List<User> list = (List<User>) getHibernateTemplate().find("from User where username='" + reader.getUsername()+"'");
        if (list.size()>0){
            return true;
        }
        return false;
    }


}
